﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Media;

namespace CastleDefender.Platform.GameScene
{
    public partial class GameScene: DrawableGameComponent
    {
        #region VARIABLE

        protected List<GameComponent> lstComponents;
        protected bool blnPause;
        protected SpriteBatch sprBatch;
        protected Animation2D aniBackground;
        protected Audio audio;

        #endregion

        #region CONSTRUCTOR

        public GameScene(Game myGame,string direction)
            : base(myGame)
        {
            sprBatch = (SpriteBatch)myGame.Services.GetService(typeof(SpriteBatch));
            lstComponents = new List<GameComponent>();
            audio = (Audio)myGame.Services.GetService(typeof(Audio));

            //Hình nền
            if (direction != "")
            {
                Texture2D textureBackground = myGame.Content.Load<Texture2D>(direction);
                aniBackground = new Animation2D(myGame, ref textureBackground, 800, 480, true, false, Vector2.Zero, 1000);
                lstComponents.Add(aniBackground);
            }

            Enabled = false;
            Visible = false;
        }
        #endregion

        #region PUBLIC METHOD

        //Hiện màn chơi
        public virtual void Show()
        {
            Enabled = true;
            Visible = true;
        }

        //Ẩn màn chơi
        public virtual void Hide()
        {
            MediaPlayer.Stop();
            Enabled = false;
            Visible = false;
        }

        //Khởi tạo các đối tượng liên quan
        public override void Initialize()
        {
            for (int i = 0; i < lstComponents.Count; i++)
            {
                lstComponents[i].Initialize();
            }

            base.Initialize();
        }

        //Cập nhật các đối tượng liên quan
        public override void Update(GameTime gameTime)
        {
            if (!blnPause && Enabled)
            {
                for (int i = 0; i < lstComponents.Count; i++)
                {
                    lstComponents[i].Update(gameTime);
                }

                base.Update(gameTime);
            }
        }

        //Vẽ các đối tượng liên quan
        public override void Draw(GameTime gameTime)
        {
            if (Enabled)
            {
                for (int i = 0; i < lstComponents.Count; i++)
                {
                    GameComponent temp = lstComponents[i];
                    if ((temp is DrawableGameComponent) && ((DrawableGameComponent)temp).Enabled)
                        ((DrawableGameComponent)temp).Draw(gameTime);
                }
                base.Draw(gameTime);
            }
        }

        #endregion

        #region PUBLIC PROPERTIES

        public List<GameComponent> Components
        {
            get { return lstComponents; }
        }
        #endregion

    }
}
